package com.xxwy.configsimple.config;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * Bootstrap 配置bean
 * @author xxwy
 * on 2018/9/28 0028
 */
@Configuration
@Component
public class MyConfiguration implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        //从ConfigurableApplicationContext获取 ConfigurableEnvironment
        ConfigurableEnvironment environment = configurableApplicationContext.getEnvironment();
        //获取PropertySources
        MutablePropertySources propertySources = environment.getPropertySources();
        //定义一个新的PropertySource,放在首位
        propertySources.addFirst(createPropertySource());
    }

    private PropertySource createPropertySource() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","xxwy");
        MapPropertySource mapPropertySource = new MapPropertySource("my-property-source",map);
        return mapPropertySource;
    }
}
